package com.firefly.admin.common.utils;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.firefly.admin.common.constant.Constants;
import com.firefly.admin.login.vo.RouterInfoVo;
import com.firefly.admin.system.entity.FySystemMenu;
import com.firefly.admin.system.entity.FySystemOrganization;
import com.firefly.admin.system.vo.MenuTree;
import com.firefly.admin.system.vo.OrgTree;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class TreeUtil {

    public static List<RouterInfoVo> makeRouter(List<FySystemMenu> menuList, Long parentId) {
        List<RouterInfoVo> routerInfoVoList = new ArrayList<>();
        Optional.ofNullable(menuList).orElse(new ArrayList<>())
                //筛选不为空的菜单及菜单父id相同的数据
                .stream().filter(menu -> menu != null && menu.getParentId() != null && menu.getParentId().longValue() == parentId.longValue())
                .forEach(menu -> {
                    RouterInfoVo routerInfoVo = new RouterInfoVo();
                    routerInfoVo.setName(menu.getMenuName());
                    routerInfoVo.setPath(menu.getMenuPath());
                    if (menu.getParentId() == Constants.MENU_TOP_NODE) {
                        routerInfoVo.setComponent("Layout");//一级菜单组件
                        routerInfoVo.setHidden(false);//显示路由
                        routerInfoVo.setRedirect("noRedirect");
                    } else {
                        routerInfoVo.setComponent(menu.getMenuPath());//具体的组件
                        routerInfoVo.setHidden(false);//折叠路由
                    }
                    routerInfoVo.setMeta(new RouterInfoVo.Meta(menu.getMenuName(),
                            menu.getMenuIcon(),
                            menu.getMenuCode().split(StringPool.COMMA), true));
                    List<RouterInfoVo> children = makeRouter(menuList, menu.getMenuId());
                    routerInfoVo.setChildren(children);
                    routerInfoVoList.add(routerInfoVo);
                });
        return routerInfoVoList;
    }

    public static List<MenuTree> makeMenuTree(List<FySystemMenu> menuList, Long parentId) {
        List<MenuTree> menuTreeList = new ArrayList<>();
        Optional.ofNullable(menuList).orElse(new ArrayList<>())
                //筛选不为空的菜单及菜单父id相同的数据
                .stream().filter(menu -> menu != null && menu.getParentId() != null && menu.getParentId().longValue() == parentId.longValue())
                .forEach(menu -> {
                    MenuTree menuTree = new MenuTree();
                    menuTree.setMenuId(menu.getMenuId());
                    menuTree.setLabel(menu.getMenuName());
                    menuTree.setIcon(menu.getMenuIcon());
                    menuTree.setMenuType(menu.getMenuType());
                    List<MenuTree> children = makeMenuTree(menuList, menu.getMenuId());
                    menuTree.setChildren(children);
                    menuTreeList.add(menuTree);
                });
        return menuTreeList;
    }

    public static List<FySystemMenu> makeMenuList(List<FySystemMenu> menuList, Long parentId) {
        List<FySystemMenu> menuTreeList = new ArrayList<>();
        Optional.ofNullable(menuList).orElse(new ArrayList<>())
                //筛选不为空的菜单及菜单父id相同的数据
                .stream().filter(menu -> menu != null && menu.getParentId() != null && menu.getParentId().longValue() == parentId.longValue())
                .forEach(menu -> {
                    FySystemMenu fySystemMenu = new FySystemMenu();
                    BeanUtils.copyProperties(menu, fySystemMenu);
                    List<FySystemMenu> children = makeMenuList(menuList, menu.getMenuId());
                    fySystemMenu.setChildren(children);
                    menuTreeList.add(fySystemMenu);
                });
        return menuTreeList;
    }

    public static List<FySystemOrganization> makeOrgList(List<FySystemOrganization> orgList, Long parentId) {
        List<FySystemOrganization> orgTreeList = new ArrayList<>();
        Optional.ofNullable(orgList).orElse(new ArrayList<>())
                //筛选不为空的组织机构及组织机构父id相同的数据
                .stream().filter(org -> org != null && org.getParentId() != null && org.getParentId().longValue() == parentId.longValue())
                .forEach(org -> {
                    FySystemOrganization fySystemOrganization = new FySystemOrganization();
                    BeanUtils.copyProperties(org, fySystemOrganization);
                    List<FySystemOrganization> children = makeOrgList(orgList, org.getOrgId());
                    fySystemOrganization.setChildren(children);
                    orgTreeList.add(fySystemOrganization);
                });
        return orgTreeList;


    }

    public static List<OrgTree> makeOrgTree(List<FySystemOrganization> orgList, Long parentId) {
        List<OrgTree> orgTreeList = new ArrayList<>();
        Optional.ofNullable(orgList).orElse(new ArrayList<>())
                //筛选不为空的菜单及菜单父id相同的数据
                .stream().filter(org -> org != null && org.getParentId() != null && org.getParentId().longValue() == parentId.longValue())
                .forEach(org -> {
                    OrgTree orgTree = new OrgTree();
                    orgTree.setOrgId(org.getOrgId());
                    orgTree.setLabel(org.getOrgName());
                    List<OrgTree> children = makeOrgTree(orgList, org.getOrgId());
                    orgTree.setChildren(children);
                    orgTreeList.add(orgTree);
                });
        return orgTreeList;
    }
}
